from datetime import datetime
from flask_sqlalchemy import SQLAlchemy

# 创建SQLAlchemy实例
db = SQLAlchemy()

# 定义环境数据类
class Data(db.Model):
    __tablename__ = "data"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    temp = db.Column(db.Float, nullable=False)      # 温度
    humidity = db.Column(db.Float, nullable=False)  # 湿度
    light = db.Column(db.Float, nullable=False)     # 光照强度
    timestamp = db.Column(db.DateTime)              # 时间戳字段
    
    # 转换为字典
    def to_dict(self):
        return {
            'id': self.id,
            'temp': self.temp,
            'humidity': self.humidity,
            'light': self.light,
            'timestamp': self.timestamp.isoformat() if self.timestamp else None
        }